<?php

namespace app\common\model;

use app\admin\model\classes\Classes;
use app\admin\model\classes\Student;
use think\Db;
use think\Model;

/**
 * 模型
 */
class ClassesSignLog extends Model
{

    protected $pk = 'id';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function getSignEndTimeAttr($val)
    {
        return $val?date('Y-m-d H:i:s', $val):'';
    }

    public function student()
    {
        return $this->hasOne(Student::class, 'uid', 'uid');
    }

    public function list($signId, $type, $status, $page=0, $limit=10)
    {
        $list = $this->search($signId, $type, $status)->field('a.*,b.student_number')->join('classes_student b', 'a.uid=b.uid')->with('student')->order('b.student_number', 'asc')->page($page, $limit)->select();
        $count = $this->search($signId, $type, $status)->count();
        return [
            'count' => $count,
            'list' => $list
        ];
    }

    public function search($signId, $type, $status)
    {
        $model = $this->alias('a')->where('a.sign_id', $signId);
        if ($type) {
            $model->where('a.type', $type);
        }

        if ($status) {
            $model->where('a.status', $status);
        }
        return $model;
    }

}
